package com.kurancy.pdf;

import java.io.FileNotFoundException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.cete.dynamicpdf.Align;
import com.cete.dynamicpdf.Document;
import com.cete.dynamicpdf.Font;
import com.cete.dynamicpdf.FontFamily;
import com.cete.dynamicpdf.Outline;
import com.cete.dynamicpdf.Page;
import com.cete.dynamicpdf.PageOrientation;
import com.cete.dynamicpdf.PageSize;
import com.cete.dynamicpdf.TextStyle;
import com.cete.dynamicpdf.VAlign;
import com.cete.dynamicpdf.WebColor;
import com.cete.dynamicpdf.XYDestination;
import com.cete.dynamicpdf.pageelements.FormattedTextArea;
import com.cete.dynamicpdf.pageelements.Group;
import com.cete.dynamicpdf.pageelements.Image;
import com.cete.dynamicpdf.pageelements.Label;
import com.cete.dynamicpdf.pageelements.Rectangle;
import com.cete.dynamicpdf.pageelements.barcoding.DataMatrixBarCode;
import com.cete.dynamicpdf.pageelements.barcoding.DataMatrixEncodingType;
import com.cete.dynamicpdf.pageelements.barcoding.DataMatrixSymbolSize;

public class KurancyPrinter
{
	private int pageCounter = 0;
	
	private Format formatter = new SimpleDateFormat("MM/dd/yy");

	public static void main( String args[] )
	{
		// Create a document and set it's properties.
		Document document = new Document();

		document.setCreator( "KurancyPrinter.java" );
		document.setAuthor( "Kurancy User" );
		document.setTitle( "My KurancyPrinter" );

		// Add the top level outline to the document
		Outline objParentOutline = document.getOutlines().add( "Top Level Outline", new XYDestination( 1, 0, 0 ) );
		objParentOutline.setColor( new WebColor( "0000FF" ) );
		objParentOutline.setStyle( TextStyle.BOLD );

		KurancyPrinter kurancyPrinter = new KurancyPrinter();

		// Add pages to the document
		kurancyPrinter.addPageElementPage( document, objParentOutline );

		// Outputs the document
		document.draw( "kurancy.pdf" );
	}

	private void addPageElementPage( Document document, Outline parentOutline )
	{
		// Increments a page counter
		pageCounter++;

//		// Adds to outlines to the page
//		Outline pageOutline = parentOutline.getChildOutlines().add( "Page " + pageCounter );
//
//		pageOutline.getChildOutlines().add( "Zoom - Fit Page", new ZoomDestination( pageCounter, PageZoom.FIT_PAGE ) );
//		pageOutline.getChildOutlines().add( "Zoom - Fit Width", new ZoomDestination( pageCounter, PageZoom.FIT_WIDTH ) );
		
		//create a new Page.
		Page page = new Page( PageSize.LETTER, PageOrientation.LANDSCAPE, 25 );
		
		buildKurancy( page.getElements(), 0, 0 );
		
		buildKurancy( page.getElements(), 375, 0 );
		
		buildKurancy( page.getElements(), 0, 190 );
		
		buildKurancy( page.getElements(), 375, 190 );
		
		buildKurancy( page.getElements(), 0, 380 );
		
		buildKurancy( page.getElements(), 375, 380 );

		// Add the page to the document
		document.getPages().add( page );
	}
	
	private void buildKurancy( Group elements, float x, float y )
	{
		addCaptionAndRectangle( elements, null, x, y, 365, 180 );
		
		elements.add( addKurancyValue( x, y ) );
		elements.add( addUserInfo( x, y ) );
		elements.add( addInstructions( x, y ) );
		elements.add( addLogo( x, y ) );
		elements.add( addBarCode( x, y ) );
	}
	
	private FormattedTextArea addKurancyValue( float x, float y )
	{
		String today = formatter.format( new Date( System.currentTimeMillis() ) );
		String amnt	 = "20";
		String code1 = "1X7895";
		String code2 = "BZ10GK";
		
		// Adds an Formatted text area to the pageElements
		String strFormattedText = 	"<p>" +
									"<b>Print Date:</b> " + today + "<br/><br/>" +
									"<b>     <font size=7>$" + amnt + "</font></b><br/><br/>" +
									"*** VERIFY THIS NOTE *** <br/><br/>" + 
									"Visit <b>kurancy.com</b> and enter this number:<br/>" + 
									"<b><font size=5>   " +	code1 + "     " + code2 + "</font></b><br/><br/>" + 
									"<b>kurancy.com</b> - IOU's that are better than cash." + 
									"</p>";
		
		FormattedTextArea objFormattedTextArea = new FormattedTextArea( strFormattedText, x + 10, y + 20, 215, 160, FontFamily.getHelvetica(), 10, true );
		
		return objFormattedTextArea;
	}
	
	private FormattedTextArea addUserInfo( float x, float y )
	{
		String issuedBy	= "M. Gaynor";
		String since 	= "04/21/12";
		String location = "Sag Harbor, NY";
		
		// Adds an Formatted text area to the pageElements
		String strFormattedText = 	"<p align=right>" +
									"<b>Issued By:</b> " + issuedBy + "<br/>" +
									"<b>Since:</b> " + since + "<br/>" +
									"<b>Location:</b> " + location +
									"</p>";
		
		FormattedTextArea objFormattedTextArea = new FormattedTextArea( strFormattedText, x + 120, y + 20, 150, 80, FontFamily.getHelvetica(), 10, true );
		
		return objFormattedTextArea;
	}
	
	private FormattedTextArea addInstructions( float x, float y )
	{
		// Adds an Formatted text area to the pageElements
		String strFormattedText = 	"<p>" +
									"How to collect?<br/>" +
									"1. Visit kurancy.com<br/>" +
									"2. Enter this number<br/>" +
									"3. Get your cash" +
									"</p>";
		
		FormattedTextArea objFormattedTextArea = new FormattedTextArea( strFormattedText, x + 175, y + 65, 150, 80, FontFamily.getHelvetica(), 9, true );
		
		return objFormattedTextArea;
	}
	
	private DataMatrixBarCode addBarCode( float x, float y )
	{
		String text = "http://www.kurancy.com";
		
		// Create a data matrix bar code
		DataMatrixBarCode dataMatrixBarCode = new DataMatrixBarCode( text, x, y + 22, DataMatrixSymbolSize.R40xC40, DataMatrixEncodingType.AUTO );
		
		dataMatrixBarCode.setXDimension( 1.75f );
		
		dataMatrixBarCode.setX( dataMatrixBarCode.getX() + ( 355 - dataMatrixBarCode.getSymbolWidth() ) );
		dataMatrixBarCode.setY( dataMatrixBarCode.getY() + ( 150 - dataMatrixBarCode.getSymbolHeight() ) );
		
		return dataMatrixBarCode;
	}
	
	private Image addLogo( float x, float y )
	{
		Image objImage = null;
		try
		{
			objImage = new Image( "src/main/resources/pdf/images/gold-letter-k.jpg", x + 340f, y + 25f, 0.24f );
		}
		catch( FileNotFoundException ex1 )
		{
			System.err.println( "Image File Not Found: " + ex1 );
		}
		
		// Image is sized and centered in the rectangle
		objImage.setBounds( 215, 60 );
		objImage.setVAlign( VAlign.TOP );
		objImage.setAlign( Align.RIGHT );

		return objImage;
	}
	
	private void addCaptionAndRectangle( Group pageElements, String caption, float x, float y, float width, float height )
	{
		// Adds a rectangle and caption to the pageElements
		Rectangle objRectangle = new Rectangle( x, y + 15, width, height - 15 );
		
		pageElements.add( objRectangle );
		
		if( caption != null )
		{
			Label objCaption = new Label( caption, x, y, 225, 10, Font.getHelveticaBold(), 10 );

			pageElements.add( objCaption );
		}
	}
	
//	private void addCaptionAndRectangle( Group pageElements, String caption, float x, float y )
//	{
//		addCaptionAndRectangle( pageElements, caption, x, y, 225, 85 );
//	}
}
